package com.aaa.marketresearch.android.widget;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.aaa.marketresearch.android.app.BillContentScreen;
import com.aaa.marketresearch.android.app.BillScreen;
import com.aaa.marketresearch.android.app.R;
import com.aaa.marketresearch.android.data.UserInfo;
import com.aaa.marketresearch.android.data.domain.BillContent;
import com.aaa.marketresearch.android.data.domain.Offer;
import com.aaa.marketresearch.android.util.SimpleLog;

import java.util.List;

/**
 * @author Nail
 */
public class BillContentAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater lInflater;
    List<BillContent> billContents;

    public BillContentAdapter(Context context, List<BillContent> billContents) {
        this.context = context;
        this.billContents = billContents;
        this.lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return billContents.size();
    }

    @Override
    public Object getItem(int position) {
        return this.billContents.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // товар по позиции
    BillContent getBillContent(int position) {
        return ((BillContent) getItem(position));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // используем созданные, но не используемые view
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.bill_content_view, parent, false);
        }

        final BillContent billContent = this.getBillContent(position);
        final Offer offer = billContent.getOffer();

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserInfo userInfo = UserInfo.getInstance();

                userInfo.setCurrentMarket(billContent.getMarket());
                userInfo.setCurrentFamilyMember(billContent.getFamilyMember());
                userInfo.setCurrentBillContent(billContent);

                Intent intent = new Intent(BillContentAdapter.this.context, BillContentScreen.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                BillContentAdapter.this.context.startActivity(intent);
            }
        });

        ((TextView) view.findViewById(R.id.offerName)).setText(offer.getName());
        ((TextView) view.findViewById(R.id.billPayedValue)).setText(billContent.getPayedValue().toString());

        Button editButton = (Button) view.findViewById(R.id.deleteBill);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SimpleLog.info("deleteBillButton");
                billContents.remove(billContent);
                ((BillScreen)BillContentAdapter.this.context).updateBillContent();
            }
        });

        return view;
    }
}